
<template>
    <div class="forumItemContainer" @click="goDetail">
        <div class="left">
            <div class="leftTitle">{{forumIt.title}}</div>
            <div class="leftUser">
                <img :src="userInfo.picUrl" alt="头像">
                <div class="leftUserName">{{userInfo.nickname}}</div>
            </div>
        </div>
        <div class="right">
            <div><!--回复-->{{forumIt.commentNum}}</div>
            <div><!--点赞-->{{forumIt.praiseNum}}</div>
            <div class="time"><!--时间--><TimeDiff :dateString="forumIt.createTime" v-if="showTime" /></div>
        </div>
    </div>
</template>

<script>
import TimeDiff from "./TimeDiff"
import identicon from 'identicon.js'
export default {
    name: 'ForumItem',
    props:['forumIt'],
    data(){
        return{
            userInfo:{},
            showTime:true
        }
    },
    components:{
        TimeDiff
    },
    mounted(){
        this.$axios({
			method: "get",
            url: `/api/users/${this.forumIt.userId}`,
			data:{

			},
            headers:{
				'Content-Type':'application/json',
				'token':localStorage.getItem('token')
			}
		}).then(res=>{
            // console.log(res)
            if(res.data.code==1){
                this.userInfo = res.data.data
                if(res.data.data.picUrl==null||res.data.data.picUrl==''){
                    this.userInfo.picUrl = 'data:image/png;base64,'+new identicon(this.md5(this.userInfo.nickname), 420).toString()
                }
            }
                
        })
    },
    methods:{
        goDetail(){
            this.$router.push({
                name:'forumDetail',
                query:{
                    postId:this.forumIt.postId
                }
            })
        }
    },
    watch:{
        forumIt(){
            this.showTime = false
             this.$axios({
                method: "get",
                url: `/api/users/${this.forumIt.userId}`,
                data:{

                },
                headers:{
                    'Content-Type':'application/json',
                    'token':localStorage.getItem('token')
                }
            }).then(res=>{
                // console.log(res)
                if(res.data.code==1){
                    this.userInfo = res.data.data
                    if(res.data.data.picUrl==null||res.data.data.picUrl==''){
                        this.userInfo.picUrl = 'data:image/png;base64,'+new identicon(this.md5(this.userInfo.nickname), 420).toString()
                    }
                }
            })
            this.$nextTick(()=>{
                this.showTime = true
            })
           
            this.$forceUpdate()
        }
    }
}
</script>
   
<style scoped>
    .forumItemContainer{
        width: 80%;
        margin-left: 10%;
        height: 80px;
        display: flex;
        flex-direction: row;
        align-items: center;
        justify-content: space-between;
        border-bottom: 1px solid #fff;
        border-top: 1px solid #fff;
    }
    .left{
        flex-direction: column;
        height: 70px;
        margin-top: 10px;
        margin-left: 10px;
        width: 70%;
    }
    .leftTitle{
        font-size: 20px;
        color: #fff;
        height: 30px;
    }
    .leftUser{
        font-size: 15px;
        color: #fff;
        height: 30px;
        display: flex;
        margin-top: 10px;
        margin-left: 8px;
    }
    .leftUser img{
        width: 20px;
        height: 20px;
        border-radius: 50%;
    }
    .leftUserName{
        margin-left: 8px;
        font-size: 10px;
        height: 20px;
        line-height: 20px;
    }
    .right{
        width: 18%;
        display: flex;
        flex-direction: row;
        align-items: center;
        justify-content: space-between;
        height: 70px;
        margin-top: 10px;
        margin-right: 10px;
        color: #fff;
        font-size: 5px;
    }
    .time{
        margin-left: -2%;
        width: 25%;
        text-align: center;
    }
    .forumItemContainer:hover .leftTitle{
        color: pink;
    }
    .forumItemContainer:hover .leftUserName{
        color: pink;
    }
</style>
